<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Allocate
 *
 * @author Schorr
 */
class AllocateController extends Zend_Controller_Action{
    //put your code here
    
     public function init()
    {
        $this->_helper->acl->allow('admin')
	    ->allow('user', array('index', 'list'));
    }
    
    public function indexAction()
    {

    }
    
    public function allocateAction()
    {
	 /* $request_from => which page calls
	 * 1 = create a new application/id -> child_report/id
	 * 2 = child report/id -> child_report/id
	 */
	$request = $this->getRequest();
	$child_id = (int) $request->getParam('id');
	$request_from = (int) $request->getParam('from');
	
	$m = new Model_Child;
	$child = $m->find($child_id);
	$this->view->children = $child;	
	
	$form = new Zend_Form;
	$form->addSubForm(new Application_Form_AllocateParent, 'parent_list');
	
	$form->addElement('submit', 'submit', array(
	    'ignore' => true,
	    'label' => 'Eltern verknüpfen',
	    ));
	$this->view->form = $form;
	
	if ($request->isPost())
	{
	    if ($form->isValid($request->getPost()))
	    {
		
		$parent_id = $form->parent_list->getValues();
		$idperson1 = $child_id;
		$idperson2 = $parent_id["parent_list"]["parent_1_id"];
		$idperson2 = (int)$idperson2;
		$data = array ('idperson1' => $idperson1 ,'idperson2' => $idperson2);
		
		$m = new Model_Allocate();
		$i = count($m->findAllocation($idperson1,$idperson2));
		if ($i == 0){$m->allocate_create($data);}
		
		//if secondary Parent select
		if ($parent_id["parent_list"]["parent_2_id"] != NULL)
		{
		    $idperson3 = $parent_id["parent_list"]["parent_2_id"];
		    $idperson3 = (int)$idperson3;
		    $data = array ('idperson1' => $idperson1 ,'idperson2' => $idperson3);
		    
		    $i = count($m->findAllocation($idperson1,$idperson3));
		    if ($i == 0){$m->allocate_create($data);}
		   
		}
		if ($request_from == 0)
		{
		    return $this->_helper->redirector->gotoURL('/child/list/');
		}
		
		// request come from create new application
		if ($request_from == 1)
		{
		    return $this->_helper->redirector->gotoURL('/child/report/id/'.$child_id);
		}
		// request come from report child
		if ($request_from == 2)
		{
		    return $this->_helper->redirector->gotoURL('/child/report/id/'.$child_id);
		}

	    }
	}
    }
    
    public function allocatedeleteAction()
 {
     	 /* $request_from => which page calls
	 * 1 = create a new application/id -> child_report/id
	 * 2 = child report/id -> child_report/id
	 */
     	$request = $this->getRequest();
	$child_id = (int) $request->getParam('id');
	$plp_id = (int) $request->getParam('plp_id');
	$request_from = (int) $request->getParam('from');
	
	
	$m = new Model_Allocate();
	$m->allocate_delete($plp_id);
	
	// request come from create new application
	if ($request_from == 1)
	{
	    return $this->_helper->redirector->gotoURL('/child/report/id/'.$child_id);
	}
	// request come from report child
	if ($request_from == 2)
	{
	    return $this->_helper->redirector->gotoURL('/child/report/id/'.$child_id);
	}
 }
 
 
}

?>
